package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.constant.TrafficTypes;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.service.SysUserService;
import com.open.capacity.utils.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.math.BigDecimal;
import java.util.*;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.StdTraffic;
import com.open.capacity.reimburse.dao.StdTrafficDao;
import com.open.capacity.reimburse.service.StdTrafficService;


@Service
public class StdTrafficServiceImpl implements StdTrafficService {

    @Autowired
    private StdTrafficDao stdTrafficDao;

    @Autowired
    private SysUserService userService;

    /**
     * 添加
     *
     * @param stdTraffic
     */
    public int save(StdTraffic stdTraffic) throws Exception {
        validition(stdTraffic);
        stdTraffic.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        stdTraffic.setUpdateTime(new Date());
        return stdTrafficDao.save(stdTraffic);
    }

    /**
     * 修改
     *
     * @param stdTraffic
     */
    public int update(StdTraffic stdTraffic) throws Exception {
        validition(stdTraffic);
        stdTraffic.setUpdateTime(new Date());
        return stdTrafficDao.update(stdTraffic);
    }

    private void validition(StdTraffic stdTraffic) throws Exception {
        StdTraffic exists = stdTrafficDao.findByLevel(stdTraffic.getLevel());
        if (exists != null && !exists.getId().equals(stdTraffic.getId())) {
            throw new Exception("不允许同一个职级配置多次");
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return stdTrafficDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public StdTraffic findById(String id) {
        return stdTrafficDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<StdTraffic> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<StdTraffic> list = stdTrafficDao.findAll(params);
        PageInfo<StdTraffic> pageInfo = new PageInfo(list);

        return PageResult.<StdTraffic>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<StdTraffic> findAll(Map<String, Object> params) {
        return stdTrafficDao.findAll(params);
    }

    @Override
    public String verify(String userId, String followUserids, TrafficTypes type, BigDecimal money) {
        String error_msg = "";
        SysUser user = userService.findById(userId);
        StdTraffic traffic = stdTrafficDao.findByLevel(user.getLevels());

        if (traffic != null) {
            switch (type) {
                case PLANE:
                    if (StringUtil.isEmpty(traffic.getPlane())) {
                        error_msg = "<p>⊙ 请严格按照交通出行标准执行，部分岗位不允许报销机票费。</p>";
                    }
                    break;
                case TRAIN:
                    break;
                case CAR:
                    break;
                case SHIP:
                    break;
            }
        }
        return error_msg;
    }
}
